package com.hangzhou.xyj.ui.widget.views;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.utils.StringUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义TextView,自己简单的封装，便于项目使用和维护
 *
 * @author neo.duan
 */
public class MyTextView extends TextView {
    private Context mContext;

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
    }

    public MyTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.mContext = context;
    }

    public MyTextView(Context context) {
        this(context, null);
        this.mContext = context;
        init();
    }

    private void init() {
        setGravity(Gravity.CENTER);
    }

    /**
     * 设置没有点击事件的文本
     *
     * @param text
     */
    public void setNoClickText(String text) {
        if (StringUtil.isBlank(text)) {
            text = "";
        }
        Matcher m = Pattern.compile("(?i)(http://|https://)[^\u4e00-\u9fa5]+").matcher(text);
        //是否匹配到含有链接的文本
        boolean isFind = false;
        while (m.find()) {
            isFind = true;
            text = text.replace(m.group(), "<a href='" + m.group() + "'>❤网页链接</a>");
        }
        //如果没有坚持到链接文本，则直接显示
        if (!isFind) {
            super.setText(text);
            return;
        }

        this.setMovementMethod(LinkMovementMethod.getInstance());
        super.setText(Html.fromHtml(text));

        CharSequence str = this.getText();
        if (str instanceof Spannable) {
            int end = str.length();
            Spannable sp = (Spannable) this.getText();
            URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(str);
            style.clearSpans();//should clear old spans
            for (URLSpan url : urls) {
                MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
                style.setSpan(myURLSpan, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                style.setSpan(new ImageSpan(mContext, R.drawable.ic_text_link), sp.getSpanStart(url), sp.getSpanStart(url) + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            super.setText(style);
            setEnabled(false);
        }
    }

    /**
     * 设置textView文本
     *
     * @param text
     */
    public void setText(String text) {
        if (StringUtil.isBlank(text)) {
            text = "";
        }
        Matcher m = Pattern.compile("(?i)(http://|https://)[^\u4e00-\u9fa5]+").matcher(text);
        //是否匹配到含有链接的文本
        boolean isFind = false;
        while (m.find()) {
            isFind = true;
            text = text.replace(m.group(), "<a href='" + m.group() + "'>❤网页链接</a>");
        }
        //如果没有坚持到链接文本，则直接显示
        if (!isFind) {
            super.setText(text);
            return;
        }

        this.setMovementMethod(LinkMovementMethod.getInstance());
        super.setText(Html.fromHtml(text));

        CharSequence str = this.getText();
        if (str instanceof Spannable) {
            int end = str.length();
            Spannable sp = (Spannable) this.getText();
            URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(str);
            style.clearSpans();//should clear old spans
            for (URLSpan url : urls) {
                MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
                style.setSpan(myURLSpan, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                style.setSpan(new ImageSpan(mContext, R.drawable.ic_text_link), sp.getSpanStart(url), sp.getSpanStart(url) + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            super.setText(style);
        }
    }

    /**
     * 处理链接
     */
    private class MyURLSpan extends ClickableSpan {
        private String mUrl;

        MyURLSpan(String url) {
            mUrl = url;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.parseColor("#11B6F0"));
            ds.setUnderlineText(false); //去掉下划线
        }

        @Override
        public void onClick(View widget) {
            IntentManager.getInstance().goWebActivity(getContext(), mUrl);
        }
    }

}
